import 'package:flutter/material.dart';
import 'package:flutter_2b_client/apis/MobileAppVersionApi.dart';
import 'package:flutter_2b_client/components/showUpdateNotesDialog.dart';
import 'package:flutter_2b_client/store/EmployeeAuthorizationStore.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart';

import '../utils/MobxHooks.dart';
import 'HomePage.dart';
import 'MinePage.dart';

class NavigationItem {
  final IconData icon;
  final String label;
  final String path;
  final Widget page;

  NavigationItem(this.icon, this.label, this.path, this.page);
}

final bottomTabBarItems = [
  NavigationItem(Icons.home, "首页", "/", const HomePage()),
  NavigationItem(Icons.person, "我的", "/mine", const MinePage()),
];

class HomeScaffold extends HookWidget {
  final Widget child;

  const HomeScaffold({Key? key, required this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    var uri = GoRouterState.of(context).uri.toString();
    var store = Provider.of<EmployeeAuthorizationStore>(context);
    var hasLogin = useObservable(() => store.hasLogin);
    useEffect(() {
      if(hasLogin){
        PackageInfo.fromPlatform().then((value) {
          mobileAppVersionApi.getLatestVersion(value.version).then((latestVersion) {
            if(latestVersion!=null){
              showUpdateNotesDialog(context,latestVersion);
            }
          });
        });
        return null;
      }
      return null;
    }, [hasLogin]);

    return Scaffold(
      body: child,
      bottomNavigationBar: NavigationBar(
        selectedIndex:
            bottomTabBarItems.indexWhere((element) => element.path == uri),
        onDestinationSelected: (value) {
          context.go(bottomTabBarItems[value].path);
        },
        destinations: bottomTabBarItems
            .map(
              (e) => NavigationDestination(icon: Icon(e.icon), label: e.label),
            )
            .toList(),
      ),
    );
  }
}
